package com.aorise.controller.bayonet;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.ServiceException;
import com.aorise.exception.SystemException;
import com.aorise.model.bayonet.*;
import com.aorise.service.bayonet.BayonetCameraService;
import com.aorise.utils.Page;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.InnerResult;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/26
 * @Description:    卡口设备模块控制器
 */
@Slf4j
@RestController
@Api(description ="卡口设备模块控制器")
@RequestMapping("api/bayonetCamera")
public class BayonetCameraController {

    @Autowired
    BayonetCameraService bayonetCameraService;

    /**
     * 卡口设备分页列表
     * HTTP 方式：GET
     * API 路径：/api/bayonetCamera/getBayonetCameraByPage
     * 方法名：getBayonetCameraByPage
     * 方法返回类型：String
     */
    @RequiresPermissions("KKSBGLFW")
    @ApiOperation(value = "卡口设备分页列表", httpMethod = "GET", response = String.class, notes = "卡口设备分页列表,提供卡口设备添加使用")
    @RequestMapping(value = "/getBayonetCameraByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetCameraByPage(BayonetCameraPageVO bayonetCameraPageVO) {
        Page bayonetPage =null;
        try {
            bayonetPage =bayonetCameraService.getBayonetCameraByPage(bayonetCameraPageVO);
        }catch (DataValidationException e) {
            log.error(
                    "controller:BayonetCameraController. function:getBayonetCameraByPage...msg: getBayonetCameraByPage DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:getBayonetCameraByPage...msg:getBayonetCameraByPage DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:BayonetCameraController. function:getBayonetCameraByPage...msg:getBayonetCameraByPage Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(bayonetPage);
    }


    /**
     * 添加卡口设备
     * HTTP 方式：POST
     * API 路径：/api/bayonetCamera/addBayonetCamera
     * 方法名：addBayonetCamera
     * 方法返回类型：String
     */
    @RequiresPermissions("KKSBGLBJ")
    @ApiOperation(value = "添加卡口设备", httpMethod = "POST", response = String.class, notes = "添加卡口设备,提供卡口设备添加使用")
    @RequestMapping(value = "/addBayonetCamera", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addBayonetCamera(@RequestBody @Validated BayonetCameraModel bayonetCamera,BindingResult result) {

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            if(bayonetCameraService.addBayonetCamera(bayonetCamera)) {
                return JsonResponseData.ofSuccess(null);
            }
            return JsonResponseData.ofFailure();
        }catch (SystemException e) {
            log.error(
                    "controller:BayonetCameraController. function:addBayonetCamera...msg: addBayonetCamera SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataValidationException e) {
            log.error(
                    "controller:BayonetCameraController. function:addBayonetCamera...msg: addBayonetCamera SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:addBayonetCamera...msg:addBayonetCamera DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetCameraController. function:addBayonetCamera...msg:addBayonetCamera Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }


    /**
     * 卡口设备详情
     * HTTP 方式：GET
     * API 路径：/api/bayonetCamera/getBayonetCameraInfo
     * 方法名：getBayonetCameraInfo
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKSBGLFW")
    @ApiOperation(value = "卡口设备详情", httpMethod = "GET", response = String.class, notes = "卡口设备详情，提供卡口设备查看编辑信息使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bayonetCameraID", value = "卡口设备ID",  paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getBayonetCameraInfo", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetCameraInfo(Integer bayonetCameraID) {
        log.debug("Request RESTful API:getBayonetInfo");
        log.debug("bayonetID {}",bayonetCameraID);
        try {
            BayonetCameraVo bayonetCameraInfo = bayonetCameraService.getBayonetCameraInfo(bayonetCameraID);
            if(bayonetCameraInfo !=null){
                return JsonResponseData.ofSuccess(bayonetCameraInfo);
            }
            return JsonResponseData.ofFailure();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:getBayonetCameraInfo...msg:getBayonetCameraInfo DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetCameraController. function:getBayonetCameraInfo...msg:getBayonetCameraInfo Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 修改卡口设备信息
     * HTTP 方式：PUT
     * API 路径：/api/bayonetCamera/upBayonetCamera
     * 方法名：upBayonetCamera
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKSBGLBJ")
    @ApiOperation(value = "修改卡口设备信息", httpMethod = "PUT", response = String.class, notes = "修改卡口设备信息，提供卡口设备修改信息使用")
    @RequestMapping(value = "/upBayonetCamera", method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String upBayonetCamera(@RequestBody @Validated BayonetCameraModel bayonetCamera,BindingResult result) {
        log.debug("Request RESTful API:upBayonet");
        log.debug("BayonetCameraModel {}",bayonetCamera);
        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            if(bayonetCameraService.upBayonetCamera(bayonetCamera)){
                return JsonResponseData.ofSuccess(null);
            }
            return JsonResponseData.ofFailure();
        }catch (SystemException e) {
            log.error(
                    "controller:BayonetCameraController. function:upBayonetCamera...msg: upBayonetCamera SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataValidationException e) {
            log.error(
                    "controller:BayonetCameraController. function:upBayonetCamera...msg: upBayonetCamera DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:upBayonetCamera...msg:upBayonetCamera DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetCameraController. function:upBayonetCamera...msg:upBayonetCamera Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 下载导入卡口设备模板
     * HTTP 方式：GET
     * API 路径：/api/bayonetCamera/download
     * 方法名：downloadBayonetCameraTemplate
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "下载导入卡口设备模板", httpMethod = "GET", response = String.class, notes = "下载导入卡口设备模板，提供卡口下载导出卡口设备信息模版使用")
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public String downloadBayonetCameraTemplate(HttpServletResponse response) {
        log.debug("Request RESTful API:downloadBayonetTemplate");
        String templateName ="bayonetCamera";
        try {
            bayonetCameraService.downloadBayonetTemplate(response,templateName);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetCameraController. function:downloadBayonetCameraTemplate...msg:downloadBayonetCameraTemplate Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        }
    }

    /**
     * 导出卡口设备信息
     * HTTP 方式：GET
     * API 路径：/api/bayonetCamera/exportBayonetCamera
     * 方法名：exportBayonetCamera
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKSBGLFW")
    @ApiOperation(value = "导出卡口设备信息", httpMethod = "GET", response = String.class, notes = "导出卡口设备信息，提供卡口导出卡口设备信息使用")
    @RequestMapping(value = "/exportBayonetCamera", method = RequestMethod.GET)
    public String exportBayonetCamera(BayonetCameraPageVO bayonetCameraPageVO,HttpServletResponse response) {
        log.debug("Request RESTful API:exportBayonet");
        try {
            bayonetCameraService.exportBayonetCamera(bayonetCameraPageVO,response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), null).toString();
        }catch (ServiceException e) {
            log.error(
                    "controller:BayonetCameraController. function:exportBayonetCamera...msg:exportBayonetCamera ServiceException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SERVICE_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SERVICE_ERROR), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:exportBayonetCamera...msg:exportBayonetCamera DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:BayonetCameraController. function:exportBayonetCamera...msg:exportBayonetCamera Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }


    /**
     * 导入卡口设备信息
     * HTTP 方式：POST
     * API 路径：/api/bayonetCamera/importBayonetCamera
     * 方法名：importBayonet
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKSBGLBJ")
    @ApiOperation(value = "导入卡口设备信息", httpMethod = "POST", response = String.class, notes = "导入卡口设备信息，提供卡口导入卡口信息使用")
    @RequestMapping(value = "/importBayonetCamera", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String importBayonetCamera(MultipartFile excelFile) {
        log.debug("Request RESTful API:importBayonet");
        try {
            InnerResult<String> result =bayonetCameraService.importBayonetCamera(excelFile);
            String msg = result.getObject().toString();
            if (result.getSuccess()) {
                return new JsonResponseData(true, msg, StatusDefine.SUCCESS, msg, null).toString();
            } else {
                return new JsonResponseData(false, msg, StatusDefine.DATA_FORMAT_ERROR, msg,
                        null).toString();
            }
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetCameraController. function:importBayonetCamera...msg:importBayonetCamera DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetCameraController. function:importBayonetCamera...msg:importBayonetCamera Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }
}
